package com.ruge.gateway.config;


import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

/**
 * 限流配置
 *
 * @author ruge.wu
 * @since 2021/12/14 12:42
 */
@Configuration
public class RateLimiterConfig {
    // 根据什么字段限流，甚至是组合字段
    @Bean
    public KeyResolver begRateLimiterKeyResolver() {
        // rt,根据字段限流，下文根据Query参数值作为限流标识，请酌情更改为自己的方式
//        return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user"));
        // URL限流
//        return exchange -> Mono.just(exchange.getRequest().getURI().getPath());
//        根据ip限流
        return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
    }

}
